//app.js
// 引入SDK核心类
var QQMapWX = require('utils/qqmap-wx-jssdk.min.js')

// 定义经纬度
var lati = null
var longi = null
const header = {
  "Content-Type":"application/json;charset=utf-8"  
}
App({
  onLaunch: function () {
    // 展示本地存储能力
    let _self = this
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.getUserInfo({
      success: function (res) {
        // console.log(res)
        _self.globalData.userSecret.iv = res.iv
        _self.globalData.userSecret.encryptedData = res.encryptedData
        _self.globalData.userInfo = res.userInfo
        if (_self.userInfoReadyCallback) {
          _self.userInfoReadyCallback(res)
        }
      },
      fail: function(){
        if (_self.userInfoReadyCallback) {
          _self.userInfoReadyCallback()
        }
      }
    })
    //获取的设备信息
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res)
      }
    })
    // 登录
    wx.login({
      success: res => {
        // console.log(res)
        this.globalData.userSecret.code = res.code
        let _self = this
        wx.authorize({
          scope: 'scope.userLocation',
          success() {
            // 获取用户地址
            wx.getLocation({
              type: 'gcj02', //返回可以用于wx.openLocation的经纬度
              success: function (res) {
                // 调用接口, 坐标转具体位置 -xxz0717
                lati = res.latitude
                longi = res.longitude
                _self.globalData.nowLocal.local = {
                  lati: res.latitude,
                  longi: res.longitude
                }
                // 实例化API核心类
                var demo = new QQMapWX({
                  key: 'OMYBZ-RE4HF-H6NJD-NM3DP-I7DQH-XHFLG' // 申请的key
                })
                // 调用接口
                demo.reverseGeocoder({
                  location: {
                    latitude: lati,
                    longitude: longi
                  },
                  success: function (res) {
                    // console.log(res)
                    _self.globalData.city = res.result.address_component.city
                    _self.globalData.nowLocal.localName = res.result.formatted_addresses.recommend
                    if (_self.localNameReadyCallback){
                      _self.localNameReadyCallback(res)
                    }
                  },
                  fail: function (res) {
                    console.log(res);
                  }
                })
                // console.log(res);
              }
            })  
          },
          fail:function(){
            _self.noLocalCallback(res)
          }
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    
  },
  //获取地理位置或用户信息失败时获取
  //注册用户
  register: function(){
    // console.log(this.globalData.userSecret)
    let data = this.globalData.userSecret
    // wx.request({
    //   url: 'http://desktop-p6dlj9v:8010/api/v1/register',
    //   data: data,
    //   header: header,
    //   method: 'POST',
    //   success: function(res){
    //     console.log(res)
    //   }
    // })
  },
  //全局数据
  globalData: {
    userInfo: null,
    chooseLocal:{},
    nowLocal:{},
    userSecret:{},
    city: '',
    isChooseAd: false
  }
})